﻿using System.Net.Mail;//
using System.Net;//

namespace PacoToolKit.Mail
{
    public delegate void SendCompletedEventHandler(System.Object sender, System.ComponentModel.AsyncCompletedEventArgs e);

    public class Mailer
    {
        /// <summary>
        /// 异步发送成功事件
        /// </summary>
        public event SendCompletedEventHandler SendCompleted;
        private SmtpClient smtpClient = new SmtpClient();
        /// <summary>
        /// 构建网络邮件发送器
        /// </summary>
        /// <param name="smtpHostName">SMTP服务地址</param>
        /// <param name="userName">账户凭据</param>
        /// <param name="password">密码凭据</param>
        public Mailer(string smtpHostName, string userName, string password)
        {
            smtpClient.Host = smtpHostName;
            smtpClient.Credentials = new NetworkCredential(userName, password);
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.SendCompleted += new System.Net.Mail.SendCompletedEventHandler(smtpClient_SendCompleted);
        }

        /// <summary>
        /// 同步发送邮件
        /// </summary>
        /// <param name="fromAddress">发出邮件地址</param>
        /// <param name="toAddress">目标邮件地址</param>
        /// <param name="subject">标题</param>
        /// <param name="encoding">编码（推荐使用UTF8）</param>
        /// <param name="body">正文（带HTML）</param>
        public void SendMail(string fromAddress, string toAddress, string subject, System.Text.Encoding encoding, string body)
        {
            MailAddress from = new MailAddress(fromAddress);
            MailAddress to = new MailAddress(toAddress);
            MailMessage message = new MailMessage(from, to);
            message.IsBodyHtml = true;
            message.Body = body;
            message.BodyEncoding = encoding;
            message.Subject = subject;
            message.SubjectEncoding = encoding;
            try
            {
                smtpClient.Send(message);
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 异步发送邮件（完成时触发SendCompleted事件）
        /// </summary>
        /// <param name="fromAddress">发出邮件地址</param>
        /// <param name="toAddress">目标邮件地址</param>
        /// <param name="userToken">用户令牌（此次发送的标志，如果不需要则填写为null）</param>
        /// <param name="subject">标题</param>
        /// <param name="encoding">编码（推荐使用UTF8）</param>
        /// <param name="html">正文（带HTML）</param>
        public void SendMailAsync(string fromAddress, string toAddress, string userToken, string subject, System.Text.Encoding encoding, string html)
        {
            MailAddress from = new MailAddress(fromAddress);
            MailAddress to = new MailAddress(toAddress);
            MailMessage message = new MailMessage(from, to);
            message.IsBodyHtml = true;
            message.Body = html;
            message.BodyEncoding = encoding;
            message.Subject = subject;
            message.SubjectEncoding = encoding;
            try
            {
                smtpClient.SendAsync(message, userToken);
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }
        void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            this.SendCompleted(sender, e);
        }
    }
}
